﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Windows.Forms;

namespace CoffeeChat.Client
{
    public class FormMain : Form
    {
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if ( disposing && ( components != null ) )
            {
                components.Dispose();
                //this.tcp.Dispose();
                //TODO: How to dispose sockets?
            }
            base.Dispose( disposing );
        }
        private void InitializeComponent()
        {
            this.buttonLoginSubmit = new System.Windows.Forms.Button();
            this.screenLogin = new System.Windows.Forms.Panel();
            this.linkShowRegister = new System.Windows.Forms.LinkLabel();
            this.textBoxLoginPassword = new System.Windows.Forms.TextBox();
            this.labelLoginPassword = new System.Windows.Forms.Label();
            this.textBoxLoginUsername = new System.Windows.Forms.TextBox();
            this.labelLoginUsername = new System.Windows.Forms.Label();
            this.screenRegister = new System.Windows.Forms.Panel();
            this.textBoxRegisterRetypePassword = new System.Windows.Forms.TextBox();
            this.labelRegisterRetypePassword = new System.Windows.Forms.Label();
            this.textBoxRegisterPassword = new System.Windows.Forms.TextBox();
            this.buttonRegisterSubmit = new System.Windows.Forms.Button();
            this.buttonRegisterCancel = new System.Windows.Forms.Button();
            this.labelRegisterPassword = new System.Windows.Forms.Label();
            this.textBoxRegisterUsername = new System.Windows.Forms.TextBox();
            this.labelRegisterUsername = new System.Windows.Forms.Label();
            this.screenMain = new System.Windows.Forms.Panel();
            this.listViewMainFriends = new System.Windows.Forms.ListView();
            this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.panelControls = new System.Windows.Forms.Panel();
            this.buttonAddFriend = new System.Windows.Forms.Button();
            this.buttonStartChat = new System.Windows.Forms.Button();
            this.labelMainWelcome = new System.Windows.Forms.Label();
            this.toolStripMain = new System.Windows.Forms.ToolStrip();
            this.menuOptions = new System.Windows.Forms.ToolStripDropDownButton();
            this.menuItemLogout = new System.Windows.Forms.ToolStripMenuItem();
            this.menuItemViewChatroom = new System.Windows.Forms.ToolStripMenuItem();
            this.screenLogin.SuspendLayout();
            this.screenRegister.SuspendLayout();
            this.screenMain.SuspendLayout();
            this.panelControls.SuspendLayout();
            this.toolStripMain.SuspendLayout();
            this.SuspendLayout();
            // 
            // buttonLoginSubmit
            // 
            this.buttonLoginSubmit.Location = new System.Drawing.Point(15, 99);
            this.buttonLoginSubmit.Name = "buttonLoginSubmit";
            this.buttonLoginSubmit.Size = new System.Drawing.Size(75, 23);
            this.buttonLoginSubmit.TabIndex = 5;
            this.buttonLoginSubmit.Text = "Login";
            this.buttonLoginSubmit.UseVisualStyleBackColor = true;
            this.buttonLoginSubmit.Click += new System.EventHandler(this.buttonLoginSubmit_Click);
            // 
            // screenLogin
            // 
            this.screenLogin.Controls.Add(this.linkShowRegister);
            this.screenLogin.Controls.Add(this.buttonLoginSubmit);
            this.screenLogin.Controls.Add(this.textBoxLoginPassword);
            this.screenLogin.Controls.Add(this.labelLoginPassword);
            this.screenLogin.Controls.Add(this.textBoxLoginUsername);
            this.screenLogin.Controls.Add(this.labelLoginUsername);
            this.screenLogin.Dock = System.Windows.Forms.DockStyle.Top;
            this.screenLogin.Enabled = false;
            this.screenLogin.Location = new System.Drawing.Point(0, 0);
            this.screenLogin.Name = "screenLogin";
            this.screenLogin.Padding = new System.Windows.Forms.Padding(5);
            this.screenLogin.Size = new System.Drawing.Size(142, 169);
            this.screenLogin.TabIndex = 0;
            // 
            // linkShowRegister
            // 
            this.linkShowRegister.AutoSize = true;
            this.linkShowRegister.Location = new System.Drawing.Point(17, 149);
            this.linkShowRegister.Name = "linkShowRegister";
            this.linkShowRegister.Size = new System.Drawing.Size(124, 13);
            this.linkShowRegister.TabIndex = 6;
            this.linkShowRegister.TabStop = true;
            this.linkShowRegister.Text = "I don\'t have a username.";
            this.linkShowRegister.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkShowRegister_LinkClicked);
            // 
            // textBoxLoginPassword
            // 
            this.textBoxLoginPassword.Dock = System.Windows.Forms.DockStyle.Top;
            this.textBoxLoginPassword.Location = new System.Drawing.Point(5, 61);
            this.textBoxLoginPassword.MaxLength = 20;
            this.textBoxLoginPassword.Name = "textBoxLoginPassword";
            this.textBoxLoginPassword.Size = new System.Drawing.Size(132, 20);
            this.textBoxLoginPassword.TabIndex = 4;
            // 
            // labelLoginPassword
            // 
            this.labelLoginPassword.Dock = System.Windows.Forms.DockStyle.Top;
            this.labelLoginPassword.Location = new System.Drawing.Point(5, 43);
            this.labelLoginPassword.Name = "labelLoginPassword";
            this.labelLoginPassword.Size = new System.Drawing.Size(132, 18);
            this.labelLoginPassword.TabIndex = 3;
            this.labelLoginPassword.Text = "Password";
            this.labelLoginPassword.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // textBoxLoginUsername
            // 
            this.textBoxLoginUsername.Dock = System.Windows.Forms.DockStyle.Top;
            this.textBoxLoginUsername.Location = new System.Drawing.Point(5, 23);
            this.textBoxLoginUsername.MaxLength = 20;
            this.textBoxLoginUsername.Name = "textBoxLoginUsername";
            this.textBoxLoginUsername.Size = new System.Drawing.Size(132, 20);
            this.textBoxLoginUsername.TabIndex = 2;
            // 
            // labelLoginUsername
            // 
            this.labelLoginUsername.Dock = System.Windows.Forms.DockStyle.Top;
            this.labelLoginUsername.Location = new System.Drawing.Point(5, 5);
            this.labelLoginUsername.Name = "labelLoginUsername";
            this.labelLoginUsername.Size = new System.Drawing.Size(132, 18);
            this.labelLoginUsername.TabIndex = 1;
            this.labelLoginUsername.Text = "Username";
            this.labelLoginUsername.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // screenRegister
            // 
            this.screenRegister.Controls.Add(this.textBoxRegisterRetypePassword);
            this.screenRegister.Controls.Add(this.labelRegisterRetypePassword);
            this.screenRegister.Controls.Add(this.textBoxRegisterPassword);
            this.screenRegister.Controls.Add(this.buttonRegisterSubmit);
            this.screenRegister.Controls.Add(this.buttonRegisterCancel);
            this.screenRegister.Controls.Add(this.labelRegisterPassword);
            this.screenRegister.Controls.Add(this.textBoxRegisterUsername);
            this.screenRegister.Controls.Add(this.labelRegisterUsername);
            this.screenRegister.Dock = System.Windows.Forms.DockStyle.Top;
            this.screenRegister.Location = new System.Drawing.Point(0, 169);
            this.screenRegister.Name = "screenRegister";
            this.screenRegister.Padding = new System.Windows.Forms.Padding(5);
            this.screenRegister.Size = new System.Drawing.Size(142, 223);
            this.screenRegister.TabIndex = 1;
            // 
            // textBoxRegisterRetypePassword
            // 
            this.textBoxRegisterRetypePassword.Dock = System.Windows.Forms.DockStyle.Top;
            this.textBoxRegisterRetypePassword.Location = new System.Drawing.Point(5, 99);
            this.textBoxRegisterRetypePassword.MaxLength = 20;
            this.textBoxRegisterRetypePassword.Name = "textBoxRegisterRetypePassword";
            this.textBoxRegisterRetypePassword.Size = new System.Drawing.Size(132, 20);
            this.textBoxRegisterRetypePassword.TabIndex = 22;
            // 
            // labelRegisterRetypePassword
            // 
            this.labelRegisterRetypePassword.Dock = System.Windows.Forms.DockStyle.Top;
            this.labelRegisterRetypePassword.Location = new System.Drawing.Point(5, 81);
            this.labelRegisterRetypePassword.Name = "labelRegisterRetypePassword";
            this.labelRegisterRetypePassword.Size = new System.Drawing.Size(132, 18);
            this.labelRegisterRetypePassword.TabIndex = 23;
            this.labelRegisterRetypePassword.Text = "Retype Password";
            this.labelRegisterRetypePassword.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // textBoxRegisterPassword
            // 
            this.textBoxRegisterPassword.Dock = System.Windows.Forms.DockStyle.Top;
            this.textBoxRegisterPassword.Location = new System.Drawing.Point(5, 61);
            this.textBoxRegisterPassword.MaxLength = 20;
            this.textBoxRegisterPassword.Name = "textBoxRegisterPassword";
            this.textBoxRegisterPassword.Size = new System.Drawing.Size(132, 20);
            this.textBoxRegisterPassword.TabIndex = 20;
            // 
            // buttonRegisterSubmit
            // 
            this.buttonRegisterSubmit.Location = new System.Drawing.Point(12, 165);
            this.buttonRegisterSubmit.Name = "buttonRegisterSubmit";
            this.buttonRegisterSubmit.Size = new System.Drawing.Size(110, 23);
            this.buttonRegisterSubmit.TabIndex = 18;
            this.buttonRegisterSubmit.Text = "Create my account!";
            this.buttonRegisterSubmit.UseVisualStyleBackColor = true;
            this.buttonRegisterSubmit.Click += new System.EventHandler(this.buttonRegisterSubmit_Click);
            // 
            // buttonRegisterCancel
            // 
            this.buttonRegisterCancel.Location = new System.Drawing.Point(12, 194);
            this.buttonRegisterCancel.Name = "buttonRegisterCancel";
            this.buttonRegisterCancel.Size = new System.Drawing.Size(75, 23);
            this.buttonRegisterCancel.TabIndex = 19;
            this.buttonRegisterCancel.Text = "Cancel";
            this.buttonRegisterCancel.UseVisualStyleBackColor = true;
            this.buttonRegisterCancel.Click += new System.EventHandler(this.buttonRegisterCancel_Click);
            // 
            // labelRegisterPassword
            // 
            this.labelRegisterPassword.Dock = System.Windows.Forms.DockStyle.Top;
            this.labelRegisterPassword.Location = new System.Drawing.Point(5, 43);
            this.labelRegisterPassword.Name = "labelRegisterPassword";
            this.labelRegisterPassword.Size = new System.Drawing.Size(132, 18);
            this.labelRegisterPassword.TabIndex = 21;
            this.labelRegisterPassword.Text = "Password";
            this.labelRegisterPassword.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // textBoxRegisterUsername
            // 
            this.textBoxRegisterUsername.Dock = System.Windows.Forms.DockStyle.Top;
            this.textBoxRegisterUsername.Location = new System.Drawing.Point(5, 23);
            this.textBoxRegisterUsername.MaxLength = 20;
            this.textBoxRegisterUsername.Name = "textBoxRegisterUsername";
            this.textBoxRegisterUsername.Size = new System.Drawing.Size(132, 20);
            this.textBoxRegisterUsername.TabIndex = 13;
            // 
            // labelRegisterUsername
            // 
            this.labelRegisterUsername.Dock = System.Windows.Forms.DockStyle.Top;
            this.labelRegisterUsername.Location = new System.Drawing.Point(5, 5);
            this.labelRegisterUsername.Name = "labelRegisterUsername";
            this.labelRegisterUsername.Size = new System.Drawing.Size(132, 18);
            this.labelRegisterUsername.TabIndex = 12;
            this.labelRegisterUsername.Text = "Username";
            this.labelRegisterUsername.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // screenMain
            // 
            this.screenMain.Controls.Add(this.listViewMainFriends);
            this.screenMain.Controls.Add(this.panelControls);
            this.screenMain.Controls.Add(this.labelMainWelcome);
            this.screenMain.Controls.Add(this.toolStripMain);
            this.screenMain.Dock = System.Windows.Forms.DockStyle.Fill;
            this.screenMain.Location = new System.Drawing.Point(0, 392);
            this.screenMain.Name = "screenMain";
            this.screenMain.Size = new System.Drawing.Size(142, 131);
            this.screenMain.TabIndex = 2;
            // 
            // listViewMainFriends
            // 
            this.listViewMainFriends.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1});
            this.listViewMainFriends.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listViewMainFriends.Location = new System.Drawing.Point(0, 51);
            this.listViewMainFriends.MultiSelect = false;
            this.listViewMainFriends.Name = "listViewMainFriends";
            this.listViewMainFriends.Size = new System.Drawing.Size(142, 16);
            this.listViewMainFriends.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.listViewMainFriends.TabIndex = 23;
            this.listViewMainFriends.UseCompatibleStateImageBehavior = false;
            this.listViewMainFriends.View = System.Windows.Forms.View.Details;
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "Your Friends";
            this.columnHeader1.Width = 300;
            // 
            // panelControls
            // 
            this.panelControls.Controls.Add(this.buttonAddFriend);
            this.panelControls.Controls.Add(this.buttonStartChat);
            this.panelControls.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panelControls.Location = new System.Drawing.Point(0, 67);
            this.panelControls.Name = "panelControls";
            this.panelControls.Size = new System.Drawing.Size(142, 64);
            this.panelControls.TabIndex = 24;
            // 
            // buttonAddFriend
            // 
            this.buttonAddFriend.Location = new System.Drawing.Point(11, 32);
            this.buttonAddFriend.Margin = new System.Windows.Forms.Padding(2);
            this.buttonAddFriend.Name = "buttonAddFriend";
            this.buttonAddFriend.Size = new System.Drawing.Size(70, 23);
            this.buttonAddFriend.TabIndex = 26;
            this.buttonAddFriend.Text = "Add Friend";
            this.buttonAddFriend.UseVisualStyleBackColor = false;
            this.buttonAddFriend.Click += new System.EventHandler(this.buttonAddFriend_Click);
            // 
            // buttonStartChat
            // 
            this.buttonStartChat.Location = new System.Drawing.Point(11, 5);
            this.buttonStartChat.Margin = new System.Windows.Forms.Padding(2);
            this.buttonStartChat.Name = "buttonStartChat";
            this.buttonStartChat.Size = new System.Drawing.Size(70, 23);
            this.buttonStartChat.TabIndex = 25;
            this.buttonStartChat.Text = "Start Chat";
            this.buttonStartChat.UseVisualStyleBackColor = true;
            this.buttonStartChat.Click += new System.EventHandler(this.buttonStartChat_Click);
            // 
            // labelMainWelcome
            // 
            this.labelMainWelcome.Dock = System.Windows.Forms.DockStyle.Top;
            this.labelMainWelcome.Location = new System.Drawing.Point(0, 25);
            this.labelMainWelcome.Margin = new System.Windows.Forms.Padding(0, 3, 0, 3);
            this.labelMainWelcome.Name = "labelMainWelcome";
            this.labelMainWelcome.Size = new System.Drawing.Size(142, 26);
            this.labelMainWelcome.TabIndex = 22;
            this.labelMainWelcome.Text = "Unknown Error 20";
            this.labelMainWelcome.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // toolStripMain
            // 
            this.toolStripMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuOptions});
            this.toolStripMain.Location = new System.Drawing.Point(0, 0);
            this.toolStripMain.Margin = new System.Windows.Forms.Padding(3);
            this.toolStripMain.Name = "toolStripMain";
            this.toolStripMain.Size = new System.Drawing.Size(142, 25);
            this.toolStripMain.TabIndex = 21;
            this.toolStripMain.Text = "toolStrip1";
            // 
            // menuOptions
            // 
            this.menuOptions.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.menuOptions.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuItemLogout,
            this.menuItemViewChatroom});
            this.menuOptions.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.menuOptions.Name = "menuOptions";
            this.menuOptions.Size = new System.Drawing.Size(57, 22);
            this.menuOptions.Text = "Options";
            // 
            // menuItemLogout
            // 
            this.menuItemLogout.Name = "menuItemLogout";
            this.menuItemLogout.Size = new System.Drawing.Size(152, 22);
            this.menuItemLogout.Text = "Logout";
            this.menuItemLogout.Click += new System.EventHandler(this.menuItemLogout_Click);
            // 
            // menuItemViewChatroom
            // 
            this.menuItemViewChatroom.Name = "menuItemViewChatroom";
            this.menuItemViewChatroom.Size = new System.Drawing.Size(152, 22);
            this.menuItemViewChatroom.Text = "View Chatroom";
            this.menuItemViewChatroom.Click += new System.EventHandler(this.menuItemViewChatroom_Click);
            // 
            // FormMain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(142, 523);
            this.Controls.Add(this.screenMain);
            this.Controls.Add(this.screenRegister);
            this.Controls.Add(this.screenLogin);
            this.MinimumSize = new System.Drawing.Size(150, 200);
            this.Name = "FormMain";
            this.Text = "Coffee Chat";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormMain_FormClosing);
            this.screenLogin.ResumeLayout(false);
            this.screenLogin.PerformLayout();
            this.screenRegister.ResumeLayout(false);
            this.screenRegister.PerformLayout();
            this.screenMain.ResumeLayout(false);
            this.screenMain.PerformLayout();
            this.panelControls.ResumeLayout(false);
            this.toolStripMain.ResumeLayout(false);
            this.toolStripMain.PerformLayout();
            this.ResumeLayout(false);

        }

        private System.Windows.Forms.Button buttonLoginSubmit;
        private System.Windows.Forms.Panel screenLogin;
        private System.Windows.Forms.TextBox textBoxLoginPassword;
        private System.Windows.Forms.Label labelLoginPassword;
        private System.Windows.Forms.TextBox textBoxLoginUsername;
        private System.Windows.Forms.Label labelLoginUsername;
        private System.Windows.Forms.LinkLabel linkShowRegister;
        private System.Windows.Forms.Panel screenRegister;
        private System.Windows.Forms.Button buttonRegisterCancel;
        private System.Windows.Forms.Button buttonRegisterSubmit;
        private System.Windows.Forms.TextBox textBoxRegisterPassword;
        private System.Windows.Forms.Label labelRegisterPassword;
        private System.Windows.Forms.TextBox textBoxRegisterRetypePassword;
        private System.Windows.Forms.Label labelRegisterRetypePassword;
        private System.Windows.Forms.Label labelRegisterUsername;
        private System.Windows.Forms.TextBox textBoxRegisterUsername;
        private System.Windows.Forms.Panel screenMain;
        private System.Windows.Forms.ListView listViewMainFriends;
        private System.Windows.Forms.ColumnHeader columnHeader1;
        private System.Windows.Forms.Panel panelControls;
        private System.Windows.Forms.Button buttonStartChat;
        private System.Windows.Forms.Button buttonAddFriend;
        private System.Windows.Forms.Label labelMainWelcome;
        private System.Windows.Forms.ToolStrip toolStripMain;
        private System.Windows.Forms.ToolStripDropDownButton menuOptions;
        private System.Windows.Forms.ToolStripMenuItem menuItemLogout;
        private System.Windows.Forms.ToolStripMenuItem menuItemViewChatroom;

        private bool _closingHandled;

        private ScreenType _currentView;

        public ScreenType CurrentView
        {
            get
            {
                return this._currentView;
            }
        }

        public FormMain()
        {
            this.InitializeComponent();
            this.SetBounds( 0, 0, 0, 400, BoundsSpecified.Height );
            this.listViewMainFriends.Columns[0].AutoResize( ColumnHeaderAutoResizeStyle.HeaderSize );

        }

        private void menuItemLogout_Click(object sender, EventArgs e)
        {
            Command cmd = new Command();
            cmd.Intent = 6;
            Program.SendData( cmd );
            this.Close();
        }
        private void menuItemViewChatroom_Click(object sender, EventArgs e)
        {
            this._channel.Show();
        }
        private void linkShowRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.SetScreens( ScreenType.Register, true );
        }
        
        private void buttonLoginSubmit_Click(object sender, EventArgs e)
        {
            string user = textBoxLoginUsername.Text;
            string pass = textBoxLoginPassword.Text;
            if ( string.IsNullOrEmpty( user ) )
            {
                MessageBox.Show( "You must type in an username!", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0 );
            }
            else
            {
                if ( string.IsNullOrEmpty( pass ) )
                {
                    MessageBox.Show( "You must type in an password!", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0 );
                }
                else
                {
                    Command cmd = new Command();
                    cmd.Intent = 4;
                    cmd.Add( "user", user );
                    cmd.Add( "pass", pass );
                    Program.SendData( cmd );
                    this.SetScreens( 0, false );
                }
            }
        }
        private void buttonAddFriend_Click(object sender, EventArgs e)
        {
            string val = Input.GetText( "Add Friend", "Type their username to save them to your friends list." );
            if ( !string.IsNullOrEmpty( val ) )
            {
                Command cmd = new Command();
                cmd.Intent = 11;
                cmd.Add( "friend", val );
                Program.SendData( cmd );
            }
        }
        private void buttonStartChat_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection friendsSelected = listViewMainFriends.SelectedItems;
            if ( friendsSelected.Count > 0 )
            {
                ChatBox tempForm = Program.GetChatBox( friendsSelected[0].Text );
                if ( tempForm != null )
                {
                    tempForm.Show();
                }
                else
                {
                    ChatBox chat = new ChatBox();
                    Program.chatBoxes.Add( chat );
                    chat.LoadChat( friendsSelected[0].Text );
                    chat.Show();
                }
            }
        }
        
        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            this._closingHandled = true;
            if ( this.tcp.IsConnected )
            {
                this.tcp.Disconnect();
            }
        }
        
        private void buttonRegisterSubmit_Click(object sender, EventArgs e)
        {
            string user = textBoxRegisterUsername.Text.Trim();
            string pass = textBoxRegisterPassword.Text;
            string pass2 = textBoxRegisterRetypePassword.Text;
            this.textBoxRegisterPassword.Text = String.Empty;
            this.textBoxRegisterRetypePassword.Text = String.Empty;
            if ( string.IsNullOrEmpty( user ) )
            {
                MessageBox.Show( "You must type in an username!", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0 );
            }
            else
            {
                if ( string.IsNullOrEmpty( pass ) )
                {
                    MessageBox.Show( "You must type in a password!", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0 );
                }
                else
                {
                    if ( !string.Equals( pass, pass2 ) )
                    {
                        MessageBox.Show( "You must retype the same password!", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0 );
                    }
                    else
                    {
                        this.SetScreens( ScreenType.Register, false );
                        Command cmd = new Command();
                        cmd.Intent = 5; //Register
                        cmd.Add( "user", user );
                        cmd.Add( "pass", pass );
                        Program.SendData( cmd );
                    }
                }
            }
        }
        private void buttonRegisterCancel_Click(object sender, EventArgs e)
        {
            this.SetScreens( 0, true );
        }


        public void UpdateFriends(Dictionary<string, bool> items)
        {
            if ( this.InvokeRequired )
            {
                this.Invoke( new UpdateFriendsHandler( this.InternalUpdateFriends ), items );
            }
            else
            {
                this.InternalUpdateFriends( items );
            }
        }
        public void InternalUpdateFriends(Dictionary<string, bool> items)
        {
            this.listViewMainFriends.Items.Clear();

            ListViewItem tempItem;
            foreach ( KeyValuePair<string, bool> item in Program.MyFriends )
            {
                tempItem = new ListViewItem();
                tempItem.Text = item.Key;
                if ( item.Value )
                {
                    tempItem.BackColor = Color.LightGreen;
                }
                else
                {
                    tempItem.BackColor = Color.FromArgb( 255, 200, 192 );
                }
                this.listViewMainFriends.Items.Add( tempItem );
            }
        }

        public void SetWelcomeMessage(string message)
        {
            if ( this.InvokeRequired )
            {
                this.Invoke( new SetWelcomeMessageHandler( this.SetWelcomeMessage ), message );
            }
            else
            {
                this.SetWelcomeMessage( message );
            }
        }
        public void InternalSetWelcomeMessage(string message)
        {
            this.labelMainWelcome.Text = message;
        }

        public void SetScreens(ScreenType viewId, bool active)
        {
            if ( this.InvokeRequired )
            {
                this.Invoke( new SetScreensHandler( this.InternalSetScreens ), viewId, active );
            }
            else
            {
                this.InternalSetScreens( viewId, active );
            }
        }
        public void InternalSetScreens(ScreenType viewId, bool active)
        {
            this._currentView = viewId;
            switch ( viewId )
            {
                case ScreenType.Login:
                    screenLogin.Enabled = active;
                    screenLogin.Visible = true;
                    screenRegister.Visible = false;
                    screenMain.Visible = false;
                    break;
                case ScreenType.Register:
                    screenRegister.Enabled = active;
                    screenLogin.Visible = false;
                    screenRegister.Visible = true;
                    screenMain.Visible = false;
                    break;
                case ScreenType.Profile:
                    screenMain.Enabled = active;
                    screenLogin.Visible = false;
                    screenRegister.Visible = false;
                    screenMain.Visible = true;
                    break;
            }
        }


    }

    public delegate void UpdateFriendsHandler(Dictionary<string, bool> items);
    public delegate void SetWelcomeMessageHandler(string message);
    public delegate void SetScreensHandler(ScreenType viewId, bool active);

    public enum ScreenType
    {
        Login,
        Register,
        Profile,
    }



}
